/**
 * 
 */
package com.gr.staffpm.pages.behaviour;

import org.apache.wicket.RequestCycle;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gr.staffpm.pages.schedule.WeekRow;

/**
 * @author Graham Rhodes 19 Mar 2011 23:09:44
 */
public class MaxDayEventsBehaviour extends AbstractDefaultAjaxBehavior {

    private static final long serialVersionUID = 1L;
    private final Logger log = LoggerFactory.getLogger(getClass());
    private final WeekRow row;

    public MaxDayEventsBehaviour(WeekRow row) {
        this.row = row;
    }

    @Override
    public void renderHead(IHeaderResponse response) {
        log.info("render jscript");
        response.renderOnLoadJavascript("var dayBodyHeight = jQuery('.ext-cal-bg-tbl').innerHeight();"
                + "var eventSpace = Math.round((dayBodyHeight - 31)/18);"
                + "console.log('Max Events: '+(dayBodyHeight - 31)/18);"
                + "wicketAjaxGet('"
                + getCallbackUrl()
                + "' + '&maxEvents='+eventSpace, function() { }, function() { });");
    }

    @Override
    protected void respond(AjaxRequestTarget target) {
        int maxEvents = Integer.parseInt(RequestCycle.get().getRequest().getParameter("maxEvents"));
        log.info("Got back max events: " + maxEvents);
        row.setMaxEvents(maxEvents, target);
    }

}
